﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace QbiconEditor
{
  public class QImageList
  {
    public ImageList ThumbnailsList
    {
      get;
      set;
    }

    public Dictionary<String, Image> OriginalImages
    {
      get;
      set;
    }

    public QImageList()
    {
      this.ThumbnailsList = new ImageList();
      this.ThumbnailsList.ColorDepth = ColorDepth.Depth32Bit;
      this.ThumbnailsList.ImageSize = new Size(QbiconConstants.DEFAULT_TEXTURES_WIDTH, QbiconConstants.DEFAULT_TEXTURES_HEIGHT);
    }

    private void FillThumbnails()
    {
      this.ThumbnailsList.Images.Clear();
      
      foreach (KeyValuePair<String, Image> img in this.OriginalImages)
      {
        this.ThumbnailsList.Images.Add(img.Key, img.Value);
      }
    }

    public void Save(string filename)
    {
      var serializer = new BinaryFormatter();

      using (var stream = File.Create(filename))
      {
        serializer.Serialize(stream, OriginalImages);
      }
    }

    public void Load(string filename)
    {
      var serializer = new BinaryFormatter();

      using (var stream = File.Open(filename, FileMode.Open))
      {
        OriginalImages = (Dictionary<string, Image>)serializer.Deserialize(stream);

        FillThumbnails();
      }
    }
  }
}
